function prt_ExportSPMmat(hfile, targetfile, opts)
% PRT::ExportSPMmat  - export an SPM mat file from a PRT
%
% FORMAT:       prt.ExportSPMmat(targetfile, opts)
%
% Input fields:
%
%       targetfile  target file name of SPM.mat (or folder)
%       opts        mandatory 1x1 struct
%                   with mandatory fields:
%        .bf        basis function, either of "hrf", "hrfd1", "hrfd2"
%        .nrvol     number of volumes per run
%        .runs      cell array of PRT objects, if only one can be omitted
%                   Note: must also contain the object used for the call!
%        .spmver    version, either 2 or 5 supported
%        .tr        TR in seconds
%                   and optional settings
%        .refbin    reference time bin (default: 1)
%        .sep       separation of runs, default: true
%        .timebins  number of time bins
%
% No output fields.
%
% Note: for SPM2 just fills the required fields and stores the file,
%       for SPM5 this function creates a jobman file instead, unless
%       the images are specified as well, in which case the SPM.mat
%       file is created with a call to spm_jobman('run', ...)

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% argument check

if nargin < 3 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'prt') || ...
   ~ischar(targetfile) || ...
    isempty(targetfile) || ...
    numel(targetfile) ~= max(size(targetfile)) || ...
    numel(opts) ~= 1 || ...
   ~isstruct(opts) || ...
   ~isfield(opts, 'bf') || ...
   ~ischar(opts.bf) || ...
    isempty(opts.bf) || ...
   ~any(strcmpi(opts.bf(:)', {'hrf', 'hrfd1', 'hrfd2'})) || ...
   ~isfield(opts, 'nrvol') || ...
   ~isa(opts.nrvol, 'double') || ...
    isempty(opts.nrvol) || ...
    any(isinf(opts.nrvol(:)) | isnan(opts.nrvol(:)) | opts.nrvol(:) < 1) || ...
    any(opts.nrvol(:) ~= fix(opts.nrvol(:))) || ...
   ~isfield(opts, 'spmver') || ...
   ~isa(opts.spmver, 'double') || ...
    numel(opts.spmver) ~= 1 || ...
   ~any([2, 5] == opts.spmver) || ...
   ~isfield(opts, 'tr') || ...
   ~isa(opts.tr, 'double') || ...
    numel(opts.tr) ~= 1 || ...
    isinf(opts.tr) || ...
    isnan(opts.tr) || ...
    opts.tr <= 0 || ...
    opts.tr > 30
    error( ...
        'BVQXfile:BadArguments', ...
        'Invalid call to PRT::ExportSPMmat.' ...
    );
end
if isfield(opts, 'runs') && ...
    iscell(opts.runs) && ...
    numel(opts.runs) == numel(opts.numvol)
    opts.runs = opts.runs(:)';
    for rc = 1:numel(opts.runs)
        if numel(opts.runs{rc}) ~= 1 || ...
           ~isBVQXfile(opts.runs{rc}, 'prt')
            error( ...
                'BVQXfile:BadArgument', ...
                'Field runs must be a 1xN cell array with PRT objects.' ...
            );
        end
    end
else
    opts.runs = {hfile};
end
if numel(opts.runs) ~= numel(opts.numvol)
    error( ...
        'BVQXfile:BadArguments', ...
        'Fields numvol and runs must match in number of elements.' ...
    );
end

% targetfile a folder?
targetfile = targetfile(:)';
if exist(targetfile, 'dir') > 0
    targetfile = [targetfile, filesep, 'SPM.mat'];
end
[tf{1:3}] = fileparts(targetfile);
